package com.unionpay.mpds.util;

import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 公共工具类
 * 
 */
public class CommonUtil {

	private static final Log log = LogFactory.getLog(CommonUtil.class);

	/**
	 * 得到String的MD5码
	 * @param srcString 将要加密码的字符串
	 * @return String 加密后的字符串
	 */
	public static String getMd5(String srcString) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(srcString.getBytes("UTF8"));
			byte s[] = md.digest();
			StringBuilder result = new StringBuilder();
			for (int i = 0; i < s.length; i++) {
				result.append(Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6));
			}
			return result.toString();
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	/**
	 * 格式化文件大小
	 * @param size
	 * @return
	 */
	public static String formatDataSize(Long dataSize) {
		double byteSize = dataSize / 1024;
		String suffix = "KB";
		if (byteSize > 1000) {
			byteSize = byteSize / 1024;
			suffix = "MB";
		}
		DecimalFormat format = new DecimalFormat("0.00");
		if ("0.00".equals(format.format(byteSize))) {
			return "0.01KB";
		}
		return format.format(byteSize) + suffix;
	}
	
	/**
	 * 格式化数字
	 * @param number 原数字
	 * @param fmt 格式 如 "0.00"
	 * @return
	 */
	public static String formatNumber(double number, String fmt) {
		DecimalFormat format = new DecimalFormat(fmt);
		return format.format(number);
	}
	
	//========================== 生成文件名 ==========================================
	/**
	 * 生成临时文件名，格式：nowTime_userId_random
	 * @param userId
	 * @return
	 */
	public static String createFileTempName(int userId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成通知附件名，格式：nowTime_orgId_userId_random
	 * @param userId
	 * @param orgId
	 * @return
	 */
	public static String createFileNoticeName(int userId, int orgId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(orgId).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成网盘附件名，格式：nowTime_orgId_userId_random
	 * @param userId
	 * @param orgId
	 * @return
	 */
	public static String createFileWebdiskName(int userId, int orgId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(orgId).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成站内信附件名，格式：nowTime_sendUserId_receiveUserId_random
	 * @param sendUserId
	 * @param receiveUserId
	 * @return
	 */
	public static String createFileLetterName(int sendUserId, int receiveUserId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(sendUserId).append("_").append(receiveUserId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成作业附件名，格式：nowTime_orgId_userId_random
	 * @param userId
	 * @param orgId
	 * @return
	 */
	public static String createFileHomeworkName(int userId, int orgId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(orgId).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成标题图片名，格式：nowTime_userId_random
	 * @param userId
	 * @return
	 */
	public static String createFileImageName(int userId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成相片名，格式：nowTime_orgId_userId_random
	 * @param userId
	 * @param orgId
	 * @return
	 */
	public static String createFilePhotoName(int userId, int orgId) {
		StringBuilder fileName = new StringBuilder();
		String nowTime = DateUtil.getStrFromDate(new Date(), "yyyyMMddHHmmss"); // 当前时间
		int random = new Random().nextInt(999999); //100W内的一个随机数
		fileName.append(nowTime).append("_").append(orgId).append("_").append(userId).append("_").append(random);
		return fileName.toString();
	}
	/**
	 * 生成个人头像名，格式：userId
	 * @param userId
	 * @return
	 */
	public static String createFilePictureName(int userId) {
		return String.valueOf(userId);
	}
	/**
	 * 生成组织logo名，格式：orgId
	 * @param orgId
	 * @return
	 */
	public static String createFileLogoName(int orgId) {
		return String.valueOf(orgId);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
